Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.ApplicationBlocks.Data
Imports DotNetNuke

Namespace ModuleMasters.DNN.Modules.NCAAPoolBracket

    Public Class SqlDataProvider

        Inherits DataProvider

#Region "Private Members"
        Private Const ProviderType As String = "data"
        Private _providerConfiguration As Framework.Providers.ProviderConfiguration = Framework.Providers.ProviderConfiguration.GetProviderConfiguration(ProviderType)
        Private _connectionString As String
        Private _providerPath As String
        Private _objectQualifier As String
        Private _databaseOwner As String
#End Region

#Region "Constructors"
        Public Sub New()

            ' Read the configuration specific information for this provider
            Dim objProvider As Framework.Providers.Provider = CType(_providerConfiguration.Providers(_providerConfiguration.DefaultProvider), Framework.Providers.Provider)

            ' Read the attributes for this provider
            If objProvider.Attributes("connectionStringName") <> "" AndAlso _
            System.Configuration.ConfigurationSettings.AppSettings(objProvider.Attributes("connectionStringName")) <> "" Then
                _connectionString = System.Configuration.ConfigurationSettings.AppSettings(objProvider.Attributes("connectionStringName"))
            Else
                _connectionString = objProvider.Attributes("connectionString")
            End If

            _providerPath = objProvider.Attributes("providerPath")

            _objectQualifier = objProvider.Attributes("objectQualifier")
            If _objectQualifier <> "" And _objectQualifier.EndsWith("_") = False Then
                _objectQualifier += "_"
            End If

            _databaseOwner = objProvider.Attributes("databaseOwner")
            If _databaseOwner <> "" And _databaseOwner.EndsWith(".") = False Then
                _databaseOwner += "."
            End If

        End Sub
#End Region

#Region "Properties"
        Public ReadOnly Property ConnectionString() As String
            Get
                Return _connectionString
            End Get
        End Property

        Public ReadOnly Property ProviderPath() As String
            Get
                Return _providerPath
            End Get
        End Property

        Public ReadOnly Property ObjectQualifier() As String
            Get
                Return _objectQualifier
            End Get
        End Property

        Public ReadOnly Property DatabaseOwner() As String
            Get
                Return _databaseOwner
            End Get
        End Property
#End Region

#Region "General Public Methods"
        Private Function GetNull(ByVal Field As Object) As Object
            Return DotNetNuke.Common.Utilities.Null.GetNull(Field, DBNull.Value)
        End Function
#End Region

#Region "NCAA Methods"

        Public Overrides Function GetUserPicks(ByVal ModuleId As Integer, ByVal UserId As Integer) As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner & ObjectQualifier & "NCAAGetUserPicks", ModuleId, UserId), IDataReader)
        End Function

        Public Overrides Function GetResults(ByVal ModuleId As Integer) As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner & ObjectQualifier & "NCAAGetResults", ModuleId), IDataReader)
        End Function

        Public Overrides Sub InsertUserPrediction(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal UserId As Integer, ByVal GameId As String, ByVal TeamName As String, ByVal GameScore As Integer, ByVal TeamAScore As Integer, ByVal TeamBScore As Integer)
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "NCAAInsertUserPrediction", PortalId, ModuleId, UserId, GameId, TeamName, GetNull(GameScore), GetNull(TeamAScore), GetNull(TeamBScore))
        End Sub

        Public Overrides Sub InsertResults(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal GameId As String, ByVal TeamName As String, ByVal GameScore As Integer, ByVal TeamAScore As Integer, ByVal TeamBscore As Integer)
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "NCAAInsertResults", PortalId, ModuleId, GameId, TeamName, GetNull(GameScore), GetNull(TeamAScore), GetNull(TeamBscore))
        End Sub

        Public Overrides Sub DeleteUserPrediction(ByVal ModuleId As Integer, ByVal UserId As Integer)
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "NCAADeleteUserPrediction", ModuleId, UserId)
        End Sub

        Public Overrides Function GetRoundPoints(ByVal PortalId As Integer, ByVal ModuleId As Integer) As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner & ObjectQualifier & "NCAAGetRoundPoints", PortalId, ModuleId), IDataReader)
        End Function

        Public Overrides Sub DeleteRoundPoints(ByVal PortalId As Integer, ByVal ModuleId As Integer)
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "NCAADeleteRoundPoints", PortalId, ModuleId)
        End Sub

        Public Overrides Sub InsertRoundPoints(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal RoundId As Integer, ByVal RoundPoints As Integer, ByVal RoundGames As Integer)
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "NCAAInsertRoundPoints", PortalId, ModuleId, RoundId, RoundPoints, RoundGames)
        End Sub

        Public Overrides Sub ClearResults(ByVal PortalId As Integer, ByVal ModuleId As Integer)
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "NCAAClearResults", PortalId, ModuleId)
        End Sub

#End Region

    End Class

End Namespace